import { createRouter, createWebHashHistory } from "vue-router";
import { webStore } from "../store/mystore";
import PropsPage from "../views/props/PropsPage.vue";
import MainPage from "../views/MainPage.vue";
import Main from "../views/main.vue";
import Login from "../views/Login.vue";
import ModelTest from "../views/vmodel/ModelTest.vue";

const routes = [
  {
    path: "/",
    component: Main,
    children: [
      {
        path: "/props",
        component: PropsPage,
      },
      {
        path: "/main",
        component: MainPage,
      },
      {
        path: "/emit",
        component: import("../views/emit/ParentEmit.vue"),
      },
      {
        path: "/inject",
        component: import("../views/provide_inject/ParentInject.vue"),
      },
      {
        path: "/slot",
        component: import("../views/slot/ParentSlot.vue"),
      },
      {
        path: "/vmodel",
        component: ModelTest,
      },
      {
        path: "/useattrs",
        component: import("../views/useattrs/index.vue"),
      },
      {
        path: "/ref_parent",
        component: import("../views/ref$parent/index.vue"),
      },
    ],
  },
  {
    path: "/login",
    component: Login,
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  let state = webStore();
  if (state.Login) {
    if (to.path != "/login") {
      next();
    } else {
      next("/");
    }
  }
  if (!state.Login) {
    if (to.path != "/login") {
      next("/login");
    } else {
      next();
    }
  }
});
export default router;
